﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using control.Dal;

namespace control.Biz
{
    public class BizPhieuXuatHang
    {
        public int ID { get; set; }
        public string Ma { get; set; }
        public DateTime NgayXuat { get; set; }
        private BizKhachHang khachhang;
        public BizKhachHang KhachHang
        {
            set { khachhang = value; }
            get
            {
                if (khachhang == null)
                    khachhang = new BizKhachHang();
                return khachhang;
            }
        }
        public string TenKhachHang
        {
            get { return KhachHang.TenKH; }
        }
        public string TenDangNhap
        {
            get { return KhachHang.TenDN; }
        }
        private List<BizChiTietPhieuXuat> items;
        public List<BizChiTietPhieuXuat> Items
        {
            set { items = value; }
            get
            {
                if (items == null)
                    items = new List<BizChiTietPhieuXuat>();
                return items;
            }
        }

        public int FindIndexItem(int ID)
        {
            for (int i = 0; i < Items.Count; i++)
            {
                if (Items[i].Sach.IdSach == ID)
                    return i;
            }
            return -1;
        }

        public string TongTien()
        {
            double resuilt = 0;
            foreach (BizChiTietPhieuXuat item in Items)
            {
                resuilt += item.ThanhTien;
            }
            return resuilt.ToString();
        }

        public void Insert()
        {
            ID = DalPhieuXuatHang.Insert(this);

        }

        public static List<BizPhieuXuatHang> GetAll()
        {
            return DalPhieuXuatHang.GetAll();
        }

        public void Delete()
        {
            DalPhieuXuatHang.Delete(this);
        }

        public static List<BizPhieuXuatHang> Find(string p, DateTime FromDateTime, DateTime ToDateTime)
        {
            DateTime newFromDateTime = new DateTime(FromDateTime.Year, FromDateTime.Month, FromDateTime.Day, 0, 0, 1);
            DateTime newToDateTime = new DateTime(ToDateTime.Year, ToDateTime.Month, ToDateTime.Day, 23, 59, 59);
            return DalPhieuXuatHang.Find(p, newFromDateTime, newToDateTime);
        }
    }
}
